﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class PressDuctTerminalOperation
	{
		public PressDuctTerminalOperation(UIDocument UiDoc, FrmPressDuctTerminal Frm)
		{
			this.uiDoc = UiDoc;
			this.doc = UiDoc.Document;
			this.frm = Frm;
		}

		public FamilySymbol layfs(Transaction tran)
		{
			FamilySymbol familySymbol = null;
			try
			{
				familySymbol = this.GetFamilySymbolyByName("AirTerminal", "加压送风口.rfa", "标准", BuiltInCategory.OST_DuctTerminal);
				tran.Commit();
			}
			catch (Exception)
			{
                if (tran != null && (int)tran.GetStatus() == 3)
				{
					tran.RollBack();
				}
			}
			HYEquipmentCommon hyequipmentCommon = new HYEquipmentCommon();
			ICollection<ElementId> allFamilyInstance = hyequipmentCommon.GetAllFamilyInstance(this.doc, BuiltInCategory.OST_DuctTerminal);
			try
			{
				this.uiDoc.PromptForFamilyInstancePlacement(familySymbol);
			}
			catch (Exception)
			{
			}
			ICollection<ElementId> paramInDuctTerminal = hyequipmentCommon.GetAllFamilyInstance(this.doc, BuiltInCategory.OST_DuctTerminal).Except(allFamilyInstance).ToList<ElementId>();
			try
			{
				Transaction transaction = new Transaction(this.doc, "设置参数");
				transaction.Start();
				try
				{
					this.setParamInDuctTerminal(paramInDuctTerminal);
				}
				catch (Exception)
				{
                    if ((int)transaction.GetStatus() == 1)
					{
						transaction.RollBack();
					}
				}
				finally
				{
                    if ((int)transaction.GetStatus() != 2)
					{
						transaction.Commit();
					}
				}
			}
			catch (Exception)
			{
			}
			return familySymbol;
		}

		private void setParamInDuctTerminal(ICollection<ElementId> collectionId2)
		{
			foreach (ElementId elementId in collectionId2)
			{
				Element element = this.doc.GetElement(elementId);
				Parameter parameter = element.GetParameter("喉口宽度");
				this.setPara(parameter, this.frm.hytbThroatWidth.Text, element);
				parameter = element.GetParameter("喉口高度");
				this.setPara(parameter, this.frm.hytbThroatHeight.Text, element);
				parameter = element.GetParameter("风口高度");
				this.setPara(parameter, this.frm.hyTerminalHeight.Text, element);
				parameter = element.GetParameter("风口宽度");
				this.setPara(parameter, this.frm.hyTerminalWidth.Text, element);
				parameter = element.GetParameter("风口厚度");
				this.setPara(parameter, this.frm.hyTerminalThickness.Text, element);
				parameter = element.GetParameter("风口风量");
				this.setPara(parameter, this.frm.hytbOneFlow.Text, element);
				bool flag = true;
				IEnumerator enumerator2 = ((FamilyInstance)element).MEPModel.ConnectorManager.Connectors.GetEnumerator();
				{
					while (enumerator2.MoveNext())
					{
						if (((Connector)enumerator2.Current).IsConnected)
						{
							flag = false;
							break;
						}
					}
				}
				if (flag)
				{
					parameter = element.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM);
					if (parameter == null)
					{
						parameter = element.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
					}
					string text = (Convert.ToDouble(this.frm.hyTextBox6.Text) + Convert.ToDouble(this.frm.hyTerminalHeight.Text) / 2000.0).ToString();
					this.setPara(parameter, text, element);
				}
			}
		}

		private void setPara(Parameter para, string text, Element ele)
		{
			if (para != null && !para.IsReadOnly)
			{
				if (para.Definition.Name == "风口风量")
				{
					double num = string.IsNullOrEmpty(text) ? 0.0 : Convert.ToDouble(text);
					para.Set(UnitConvert.CovertToAPI(num * 5.0 / 18.0, (Autodesk.Revit.DB.DisplayUnitType)64));
					return;
				}
				if (para.Definition.Name == "偏移量" || para.Definition.Name == "偏移")
				{
					if (!(this.doc.ActiveView is View3D))
					{
						foreach (Parameter parameter in ele.GetParameters("标高").ToList<Parameter>())
						{
							if (parameter != null && !parameter.IsReadOnly)
							{
								parameter.Set(new ElementId(this.doc.ActiveView.GenLevel.Id.IntegerValue));
								break;
							}
						}
					}
					para.Set(NumbericTools.mmToFeet(string.IsNullOrEmpty(text) ? 0.0 : Convert.ToDouble(text)) * 1000.0);
					return;
				}
				para.Set(NumbericTools.mmToFeet(string.IsNullOrEmpty(text) ? 0.0 : Convert.ToDouble(text)));
			}
		}

        public FamilySymbol GetFamilySymbolyByName(string typeName, string familyName, string symbolName = "标准", BuiltInCategory built = (BuiltInCategory)(-2008013))
		{
			FamilySymbol result = null;
			string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(this.doc), typeName, familyName);
			Family family = null;
			SubTransaction subTransaction = null;
			try
			{
				foreach (Element element in YJKElementFilter.GetElementByCategory(this.doc, typeof(FamilySymbol), built))
				{
					if ((element as FamilySymbol).Family.Name == familyName.Substring(0, familyName.IndexOf(".")) && (element as FamilySymbol).Name == symbolName)
					{
						FamilySymbol result2;
						result = (result2 = (element as FamilySymbol));
						return result2;
					}
				}
				subTransaction = new SubTransaction(this.doc);
				subTransaction.Start();
                this.doc.LoadFamily(text, new MyFamilyLoadOptions(), out family);
				subTransaction.Commit();
			}
			catch (Exception)
			{
			}
			finally
			{
				if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
				{
					subTransaction.RollBack();
				}
			}
			if (family != null)
			{
				foreach (FamilySymbol familySymbol in family.GetSymbols())
				{
					if (familySymbol.Family.Name == familyName.Substring(0, familyName.IndexOf(".")) && familySymbol.Name == symbolName)
					{
						result = familySymbol;
						break;
					}
				}
			}
			return result;
		}

		private Document doc;

		private UIDocument uiDoc;

		private FrmPressDuctTerminal frm;
	}
}
